package com.soft.javase.day12.ticket;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/19
 */
public class TicketSystem {
    private int ticketCount;

    public TicketSystem(int totalTicket) {
        this.ticketCount = totalTicket;
        System.out.println("售票系统总票数: " + totalTicket);
    }

    public int sellTicket() {
        synchronized (this) {
            if (ticketCount > 0) {
                int soldTicket = ticketCount--;
                System.out.println(Thread.currentThread().getName()
                        + " 售出票号: " + soldTicket + "，剩余票数: " + ticketCount);
                return soldTicket;
            } else {
                System.out.println(Thread.currentThread().getName()
                        + " 尝试购票，但票已售完");
                return 0;
            }
        }
    }
}
